package com.luge.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;

public class FileManager {
	public static FileManager mFileManager = null;

	private FileManager() {

	}

	public static FileManager getInstance() {
		if (mFileManager == null) {
			mFileManager = new FileManager();
		}
		return mFileManager;
	}

	public File creatFile(String fileName) throws IOException {
		File file = new File(fileName);
		file.createNewFile();
		return file;
	}
	public File creatDir(String dirName) {
		File dir = new File(dirName);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return dir;
	}

	public boolean isFileExist(String fileName) {
		if (fileName == null || fileName.equals("")) {
			return false;
		}
		File file = new File(fileName);
		if (!file.exists()) {
			return false;
		} else if (file.length() < 100) {
			file.delete();
			return false;
		} else {
			return true;
		}
	}

	public boolean isNetFileExist(String fileName, String urlStr) {
		if (fileName == null || fileName.equals("") || urlStr == null
				|| !urlStr.contains(".")) {
			return false;
		}
		File file = new File(fileName);
		if (!file.exists()) {
			return false;
		} else if (!file.isFile()) {
			return false;
		} else if (file.length() == 0) {
			file.delete();
			return false;
		} else if (getUrlFileSize(urlStr) == file.length()) {
			return true;
		} else {
			return false;
		}
	}

	public int getUrlFileSize(String urlPath) {
		URL url = null;
		HttpURLConnection httpURLConnection = null;
		try {
			url = new URL(urlPath);
			httpURLConnection = (HttpURLConnection) url.openConnection();
			return httpURLConnection.getContentLength();
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} finally {
			if (httpURLConnection != null) {
				httpURLConnection.disconnect();
				httpURLConnection = null;
			}
			if (url != null) {
				url = null;
			}
		}
	}

	public boolean downloadImgFromUrl(String path, String fileName,
			String urlStr) {
		File file = null;
		try {
			InputStream inputStream = StreamTool.getInputStream(urlStr);
			creatDir(path);
			file = creatFile(fileName);
			StreamTool.writeInputStream(file, inputStream);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public int getFileSizes(String filePath) throws Exception {
		File mFile = new File(filePath);
		if (mFile.exists()) {
			return (int) mFile.length();
		}
		return 0;
	}

	public long getFileSize(File dir) throws Exception {
		long size = 0;
		File flist[] = dir.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}

	public String formatFileSize(long fileSize) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileSize == 0) {
			fileSizeString = "0";
		} else if (fileSize < 1024) {
			fileSizeString = df.format((double) fileSize) + "B";
		} else if (fileSize < 1048576) {
			fileSizeString = df.format((double) fileSize / 1024) + "K";
		} else if (fileSize < 1073741824) {
			fileSizeString = df.format((double) fileSize / 1048576) + "M";
		} else {
			fileSizeString = df.format((double) fileSize / 1073741824) + "G";
		}
		return fileSizeString;
	}

	public long getlist(File dir) {
		long size = 0;
		File flist[] = dir.listFiles();
		size = flist.length;
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getlist(flist[i]);
				size = size - 1;
			}
		}
		return size;
	}

	public void deleteDir(String dir) {
		File[] allFile = new File(dir).listFiles();
		for (File file : allFile) {
			if (file.isFile()) {
				file.delete();
			}
		}
	}

	public void deleteFile(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	public int includeFile(String filePath) {
		File[] allFile = new File(filePath).listFiles();
		int i = 0;
		for (File file : allFile) {
			if (file.isFile()) {
				i++;
			}
		}
		return i;
	}

}
